<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }

    a {
      text-decoration: none;
    }

    .xtx-cart-page .container {
      width: 1200px;
      margin: 50px auto;
      border: 3px solid #ccc;
    }

    .xtx-cart-page h3 {
      text-align: center;
      font-size: 30px;
      margin: 20px 0;
    }

    .tc {
      text-align: center;
    }

    .tc .xtx-numbox {
      margin: 0 auto;
      width: 120px;
    }

    .red {
      color: #db2828;
    }
    .red::before {
      content: "￥";
      font-size: 18px;
    }

    .green {
      color: #32aa66;
    }

    .f16 {
      font-size: 16px;
    }

    .goods {
      display: flex;
      align-items: center;
    }

    .goods img {
      width: 100px;
      height: 100px;
    }

    .goods>div {
      width: 280px;
      font-size: 16px;
      padding-left: 10px;
    }

    .goods>div .attr {
      font-size: 14px;
      color: #999;
    }

    .action {
      display: flex;
      background: #fff;
      margin-top: 20px;
      height: 80px;
      align-items: center;
      font-size: 16px;
      justify-content: space-between;
      padding: 0 30px;
    }

    .action .xtx-checkbox {
      color: #999;
    }

    .action .batch a {
      margin-left: 20px;
    }

    .action .red {
      font-size: 18px;
      margin-right: 20px;
      font-weight: bold;
    }

    .tit {
      color: #666;
      font-size: 16px;
      font-weight: normal;
      line-height: 50px;
    }

    .xtx-cart-page .cart {
      background: #fff;
      color: #666;
    }

    .xtx-cart-page .cart table {
      border-spacing: 0;
      border-collapse: collapse;
      line-height: 24px;
    }

    .xtx-cart-page .cart table th,
    .xtx-cart-page .cart table td {
      padding: 10px;
      border-bottom: 1px solid #f5f5f5;
    }

    .xtx-cart-page .cart table th:first-child,
    .xtx-cart-page .cart table td:first-child {
      text-align: left;
      padding-left: 30px;
      color: #999;
    }

    .xtx-cart-page .cart table th {
      font-size: 16px;
      font-weight: normal;
      line-height: 50px;
    }

    .box {
      width: 1200px;
      margin: 0 auto;
    }

    .box p {
      height: 30px;
      line-height: 30px;
    }
  </style>
</head>

<body>

  <!-- ---------------------- 代码区域 ---------------------------- -->
  <div class="xtx-cart-page">
    <div class="container">
      <div class="cart">
        <h3>确认订单</h3>
        <table>
          <thead>
            <tr>
              <th width="120">序号</th>
              <th width="400">商品信息</th>
              <th width="220">单价</th>
              <th width="180">数量</th>
              <th width="180">小计</th>
              <th width="140">操作</th>
            </tr>
          </thead>
          <!-- 商品列表 -->
          <tbody>

            <!-- <tr>
              <td>i</td>
              <td>
                <div class="goods">
                  <a href="#">
                    <img src="https://yanxuan-item.nosdn.127.net/6aaff130c1d97c60f9931e00734bbad6.png" alt="">
                  </a>
                  <div>
                    <p class="name">
                      共享品茶乐趣公道杯闻香杯手工吹制
                    </p>
                    <p class="attr">
                      规格：公道杯230ml/手工吹制
                    </p>
                  </div>
                </div>
              </td>
              <td class="tc">
                <p> &yen;89.00</p>
              </td>
              <td class="tc">
                <p> 10</p>
              </td>
              <td class="tc">
                <p class="f16 red"> &yen; 890.00 </p>
              </td>
              <td class="tc">
                <p > <a class="green" href="javascript:;">删除</a> </p>
              </td>

            </tr> -->

          </tbody>
        </table>
      </div>
      <!-- 操作栏 -->
      <div class="action">
        <!-- <div class="batch"></div>
        <div class="total">
          共 9 件商品，商品合计：
          <span class="red">¥1800</span>
        </div> -->
      </div>
    </div>
  </div>
  <script src="./data.json"></script>
  <script>
    // console.log(arr);

    const tbody = document.querySelector(".cart tbody")
    const action = document.querySelector(".action")
    // console.log(tbody);


    // 根据arr数组数据动态渲染 tbody  封装一个渲染函数  render()  页面渲染

    function render() {
      const strArr = arr.map((item, index) => {
        // console.log(item);
        // console.log(index);
        // 对象的解构赋值
        const { picture, name, attrsText, price, count } = item;
        return `
          <tr>
            <td>${index + 1}</td>
            <td>
              <div class="goods">
                <a href="#/"
                  ><img
                    src="${picture}"
                    alt=""
                /></a>
                <div>
                  <p class="name ellipsis">
                    ${name}
                  </p>
                  <p class="attr">${attrsText}</p>
                </div>
              </div>
            </td>
            <td class="tc">
              <p>&yen;${price}</p>
            </td>
            <td class="tc">${count}</td>
            <td class="tc"><p class="f16 red">${(count * price).toFixed(2)}</p></td>
            <td class="tc">
              <p><a data-index="${index}" class="green" href="javascript:;">删除</a></p>
            </td>
          </tr>
            `
          }).join("")

          // console.log(strArr);

          tbody.innerHTML = strArr;

      }

      render()


       // 根据 arr 数据动态计算 总数量 和 总价
      function renderPrice() {
        // 分类汇总
        // const totalCount =  arr.reduce((sum,item)=>{
        //     return sum+item.count
        // },0)
          let totalCount = 0;
          for (let i = 0; i < arr.length; i++) {
            //  console.log(arr[i]);
             totalCount += arr[i].count 
          }
          console.log(totalCount);
          


        // console.log(totalCount);
        // const totalPrice =  arr.reduce((sum,item)=>{
        //     return sum+item.count * item.price
        // },0)
        let totalPrice = 0;
        for (let i = 0; i < arr.length; i++) {
            //  console.log(arr[i]);
            totalPrice += arr[i].count * arr[i].price;
        }
        console.log(totalPrice.toFixed(2));
        action.innerHTML = `
          <div class="batch"></div>
          <div class="total">
            共 ${totalCount} 件商品，商品合计：
            <span class="red">${totalPrice}</span>
          </div>
        
        `
      }
      renderPrice()

      // 事件委托，注册删除事件
      tbody.addEventListener("click",function (event) {
          console.log(event);
          if (event.target.tagName = "A") {
            const index = event.target.dataset.index
            console.log(index);
            arr.splice(index,1)
            render();
            renderPrice()
          }
          
      })

  </script>
</body>

</html>